<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="器件名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入器件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编号" prop="materialNumber">
        <el-input
          v-model="queryParams.materialNumber"
          placeholder="请输入物料编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="库存状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="库存状态" clearable>
          <el-option
            v-for="dict in dict.type.lw_warehouse_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="型号/规格" prop="model">-->
<!--        <el-input-->
<!--          v-model="queryParams.model"-->
<!--          placeholder="请输入型号/规格"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="供应商" prop="encapsulation">-->
<!--        <el-input-->
<!--          v-model="queryParams.encapsulation"-->
<!--          placeholder="请输入供应商"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="数量" prop="quantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.quantity"-->
<!--          placeholder="请输入数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['materials:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['materials:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['materials:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleExport"
          v-hasPermi="['materials:order:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleImport"
          v-hasPermi="['materials:order:import']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="原材料id" align="center" prop="orderId" />-->
      <el-table-column label="器件名称" align="center" prop="name" />
      <el-table-column label="物料编号" align="center" prop="materialNumber" />
      <el-table-column label="型号/规格" align="center" prop="model" />
      <el-table-column label="封装" align="center" prop="encapsulation" />
      <el-table-column label="供应商" align="center" prop="supplier" />
      <el-table-column label="库存状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.lw_warehouse_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="下单" align="center" prop="quantity" />
      <el-table-column label="到货" align="center" prop="actualArrival" />
      <el-table-column label="库存" align="center" prop="inventory" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-shopping-cart-2"
            @click="outStorage(scope.row)"
            v-hasPermi="['materials:order:out']"
          >交付</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-shopping-cart-1"
            @click="handOverTo(scope.row)"
            v-hasPermi="['materials:order:handOverTo']"
          >转交</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-shopping-cart-full"
            @click="putStorage(scope.row)"
            v-hasPermi="['materials:order:put']"
          >下单</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['materials:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['materials:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 转交原材料对话框 -->
    <el-dialog :title="title" :visible.sync="openhandOverForm" width="500px" append-to-body>
      <el-form ref="handOverForm" :model="handOverForm" :rules="rules" label-width="80px">
        <el-form-item label="已到货" prop="actualArrival">
          <el-input v-model="handOverForm.actualArrival" :disabled = "true"/>
        </el-form-item>
        <el-form-item label="到货" prop="remove">
          <el-input v-model="handOverForm.remove" placeholder="请输入交付数量" />
        </el-form-item>
        <el-form-item label="备注" prop="infoRemark">
          <el-input type="textarea" v-model="handOverForm.infoRemark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHandOverForm">操作</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 出入库原材料对话框 -->
    <el-dialog :title="title" :visible.sync="openOrderForm" width="500px" append-to-body>
      <el-form ref="orderForm" :model="orderForm" :rules="rules" label-width="80px">
        <el-form-item label="已下单" prop="quantity">
          <el-input v-model="orderForm.quantity" :disabled = "true"/>
        </el-form-item>
        <el-form-item label="下单" prop="variable" v-if="flash">
          <el-input v-model="orderForm.variable" placeholder="请输入下单数量" />
        </el-form-item>
        <el-form-item label="到货" prop="remove" v-if="!flash">
          <el-input v-model="orderForm.remove" placeholder="请输入交付数量" />
        </el-form-item>
        <el-form-item label="备注" prop="infoRemark">
          <el-input type="textarea" v-model="orderForm.infoRemark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitOrderForm">操作</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改原材料订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="器件名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入器件名称" />
        </el-form-item>
        <el-form-item label="物料编号" prop="materialNumber">
          <el-input v-model="form.materialNumber" placeholder="请输入物料编号" />
        </el-form-item>
        <el-form-item label="型号/规格" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号/规格" />
        </el-form-item>
        <el-form-item label="封装" prop="encapsulation">
          <el-input v-model="form.encapsulation" placeholder="请输入封装" />
        </el-form-item>
        <el-form-item label="供应商" prop="supplier">
          <el-input v-model="form.supplier" placeholder="请输入封装" />
        </el-form-item>
<!--        <el-form-item label="数量" prop="quantity">-->
<!--          <el-input v-model="form.quantity" placeholder="请输入数量" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
<!--        <div class="el-upload__tip" slot="tip">-->
<!--          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据-->
<!--          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>-->
<!--        </div>-->
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listOrder,
  getOrder,
  delOrder,
  addOrder,
  updateOrder,
  updateOrderQuantity,
  handOverTOActualArrival
} from "@/api/materials/order";
import dict from "@/utils/dict";
import { getToken } from "@/utils/auth";

export default {
  name: "Order",
  dicts: ['lw_warehouse_status'],
  data() {
    return {
      flash: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原材料订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openOrderForm: false,
      openhandOverForm: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        materialNumber: null,
        model: null,
        encapsulation: null,
        supplier: null,
        status: null,
        quantity: null,
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/materials/order/importData"
      },
      // 表单参数
      form: {},
      orderForm: {},
      handOverForm:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 下单按钮操作 */
    putStorage(row) {
      this.reset();
      this.flash = true;
      const orderId = row.orderId;
      getOrder(orderId).then(response => {
        this.orderForm = response.data;
        this.orderForm.remove = 0;
        this.openOrderForm = true;
        this.title = "物料下单";
      });
    },
    /** 下单交付提交按钮 */
    submitOrderForm() {
      this.$refs["orderForm"].validate(valid => {
        if (valid) {
          if (this.orderForm.orderId != null) {
            updateOrderQuantity(this.orderForm).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.openOrderForm = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 下单转交提交按钮 */
    submitHandOverForm(){
      this.$refs["handOverForm"].validate(valid => {
        if (valid) {
          if (this.handOverForm.orderId != null) {
            handOverTOActualArrival(this.handOverForm).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.openhandOverForm = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 转交按钮操作 */
    handOverTo(row) {
      this.reset();
      this.flash = false;
      const orderId = row.orderId;
      getOrder(orderId).then(response => {
        this.handOverForm = response.data;
        this.handOverForm.remove = 0;
        this.openhandOverForm = true;
        this.title = "物料转交";
      });
    },
    /** 出库按钮操作 */
    outStorage(row) {
      this.reset();
      this.flash = false;
      const orderId = row.orderId;
      getOrder(orderId).then(response => {
        this.orderForm = response.data;
        this.orderForm.remove = 0;
        this.orderForm.variable = 0;
        this.openOrderForm = true;
        this.title = "物料交付";
      });
    },
    /** 查询原材料订单列表 */
    getList() {
      console.log(this.queryParams);
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openOrderForm = false;
      this.openhandOverForm = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderId: null,
        name: null,
        materialNumber: null,
        model: null,
        encapsulation: null,
        supplier: null,
        status: null,
        quantity: null,
        remark: null
      };
      this.resetForm("orderForm");
      this.orderForm = {
        orderId: null,
        name: null,
        materialNumber: null,
        model: null,
        encapsulation: null,
        supplier: null,
        quantity: null,
        remark: null,
        variable:0,
        remove: 0,
        infoRemark: null,
      };
      this.resetForm("orderForm");
      this.handOverForm = {
        orderId: null,
        name: null,
        materialNumber: null,
        model: null,
        encapsulation: null,
        supplier: null,
        quantity: null,
        remark: null,
        variable:0,
        remove: 0,
        infoRemark: null,
        actualArrival: null,
      };
      this.resetForm("handOverForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加原材料订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const orderId = row.orderId || this.ids
      getOrder(orderId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改原材料订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.orderId != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const orderIds = row.orderId || this.ids;
      this.$modal.confirm('是否确认删除原材料订单编号为"' + orderIds + '"的数据项？').then(function() {
        return delOrder(orderIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('materials/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "订单导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('materials/order/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
// 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
// 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
// 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
